package models;

import models.constants.DeletedStatus;
import utils.DateUtil;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;

/**
 * Created by liming on 2016/12/26.
 */
@Entity
@Table(name = "admin_stock_in_plan_setup")
public class AdminStockInPlanSetup extends BaseModel {
    /**
     * 采购计划编号
     */
    @Column(name = "code")
    public String code;

    /**
     * 创建日期
     */
    @Transient
    public String createAtStr;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;

    /**
     * s
     * 删除状态
     */
    @Column(name = "deleted_status")
    public DeletedStatus deletedStatus;


    /**
     * 获取当天的采购数量设置表
     * 自动生成采购单,如果生成时间在每天晚上凌晨00:00点之前,取今天的的日期 new Date(),
     * 如果在凌晨00:00之后,取昨天的日期 DateUtil.getYesterday().
     *
     * @return
     */
    public static AdminStockInPlanSetup getByCreateAt() {
        String today = DateUtil.dateToString(DateUtil.getYesterday(), "yyyy-MM-dd");
        return AdminStockInPlanSetup.find(" deletedStatus = ? and  createAt like '" + today + "%'" , DeletedStatus.UN_DELETED).first();
    }
    //根据id查询采购计划单
    public  static  AdminStockInPlanSetup findByInPlanSetup(long id){
        return AdminStockInPlanSetup.find(" id=? and deletedStatus=?",id,DeletedStatus.UN_DELETED).first();

    }
    //根据创建时间查询采购计划单
    public  static  AdminStockInPlanSetup findByInPlanSetup(Date date){
        String today=DateUtil.dateToString(date,"yyyy-MM-dd");
        return AdminStockInPlanSetup.find(" createAt like '" + today + "%' and deletedStatus='"+DeletedStatus.UN_DELETED+"'").first();

    }
}
